<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            控制台
            <small>控制面板</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">资讯分类管理</li>
            <li class="active">资讯分类列表</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title">资讯分类列表</h3>
                <!--<a href="#" class="btn  btn-primary pull-right">添加分类</a>-->
                <button type="button" class="btn btn-primary pull-right" onclick="addCate(this,0)">添加分类</button>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <table class="table table-bordered center"><tbody>
                <tr>
                    <th style="width: 10px">
                        <label for="checkall" class="btn btn-default btn-sm">
                            <input type="checkbox" value="" id="checkall" class="pointer">
                            全选
                        </label>
                    </th>
                    <th>标题</th>
                    <th style="width: 100px;">操作</th>
                </tr>
                <?php foreach($infocate as $item){?>
                <tr>
                    <td>
                        <label class="lb">
                            <input type="checkbox" class="cbox"  value="" name="idarr[]">
                        </label>
                    </td>
                    <td><?php echo $item['cat_title']; ?></td>
                    <td >
                        <button class="btn btn-info btn-sm" onclick="addCate(this,<?php echo $item['cat_id']; ?>)"><i class="fa fa-edit"></i></button>
                        <a href="{:url('/admin/infocate/del/cid/'.$item['cat_id'])}" onclick="return confirm('确定要删除码？');" class="btn btn-sm btn-danger"><i class="fa fa-trash-o"></i></a>
                    </td>
                </tr>
                <?php } ?>
                </tbody></table>
            </div>
            <!-- /.box-body -->
            <div class="box-footer clearfix">
                <button type="button" class="btn btn-danger"><i class="fa fa-trash-o"></i> 删除</button>
                <ul class="pagination pagination-sm no-margin pull-right">
                    <li><a href="#">«</a></li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">»</a></li>
                </ul>
            </div>
        </div>
    </section>
    <!-- /.content -->
    <!--弹出层开始-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">添加分类</h4>
                </div>
                <form class="form-horizontal" id="formData" style="margin-top: 15px">
                    <input id="inputcid" type="hidden" name="cid">
                    <div class="form-group">
                        <label for="inputCate" class="col-sm-2 control-label">分类标题</label>
                        <div class="col-sm-9">
                            <input type="text" name="cat_title" class="form-control" id="inputCate">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <input type="button" class="btn btn-primary" onclick="update()" value="保存">
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!--弹出层结束-->
    <!--小型模态框-->
    <div class="modal fade bs-example-modal-sm" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" style="display: none;">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <h4 class="modal-title" id="mySmallModalLabel">Small modal</h4>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
</div>
<script type="text/javascript">
    //触发模态框的同时调用此方法
    function addCate(obj,type){
        if (type==0){
            $('#exampleModalLabel').text('添加分类');
            $('#inputcid').val(0);
        }else{
            $('#exampleModalLabel').text('编辑分类');
            $('#inputcid').val(type);
            var tds= $(obj).parent().find('td');
            $('#inputCate').val($(obj).parent().siblings().eq(1).text());
        }

        $('#exampleModal').modal('show');
    }
    //提交更改
    function update() {
        //获取模态框数据
        var data = $('#formData').serializeArray();
        $.ajax({
            type: "post",
            url: "{:url('/admin/infocate/cateEdit')}",
            data: data,
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            success: function(result) {
                $('#exampleModal').modal('hide');
                if (parseInt(result)){
                    // 保存成功
                    $('#mySmallModalLabel').text('保存成功!');
                    $('#smallModal').modal('show');
                } else{
                    //保存失败
                    $('#mySmallModalLabel').text('保存失败!');
                    $('#smallModal').modal('show');
                }
                setTimeout(function(){
                    $('#smallModal').modal('hide');
                    window.location = "{:url('/admin/infocate/')}";
                },3000);
            }
        });
    }

</script>